<template>
  <component
    is="a-modal"
    v-model:visible="visible"
    :width="tool.getDevice() === 'mobile' ? '100%' : '600px'"
    :title="title"
    :mask-closable="false"
    :ok-loading="loading"
    @cancel="close"
    @before-ok="submit">
    <!-- 表单信息 start -->
    <a-form ref="formRef" :model="formData" :rules="rules" :auto-label-width="true">
      <a-form-item label="回放时长/天" field="play_back_days">
        <a-input v-model="formData.play_back_days" placeholder="请输入回放天数" />
      </a-form-item>
      <a-form-item label="弹幕按钮是否显示" field="barrage_button">
        <sa-radio v-model="formData.barrage_button" dict="data_open" />
      </a-form-item>
      <a-form-item label="进度条" field="progress_bar">
        <sa-radio v-model="formData.progress_bar" dict="data_open" />
      </a-form-item>
      <a-form-item label="倍速" field="speed">
        <sa-radio v-model="formData.speed" dict="data_open" />
      </a-form-item>
      <a-form-item label="自动播放" field="autoplay">
        <sa-radio v-model="formData.autoplay" dict="data_open" />
      </a-form-item>
    </a-form>
    <!-- 表单信息 end -->
  </component>
</template>

<script setup>
import { ref, reactive, computed } from 'vue'
import { Message } from '@arco-design/web-vue'
import commonApi from '@/api/common'
import tool from '@/utils/tool'
import api from '@/api/live/live'

const emit = defineEmits(['success'])

// 引用定义
const formRef = ref()
const mode = ref('')
const visible = ref(false)
const loading = ref(false)

let title = computed(() => {
  return '回放设置'
})

// 表单初始值
const initialFormData = {
  room_id: '',
  barrage_button:1,
  progress_bar:1,
  speed:1,
  autoplay:1,
  play_back_days:'',
}

// 表单信息
const formData = reactive({ ...initialFormData })

// 验证规则
const rules = {
  play_back_days: [{ required: true, message: '请输入持续时长' }],
}

// 打开弹框
const open = async (type = 'add') => {
  mode.value = type
  // 重置表单数据
  Object.assign(formData, initialFormData)
  formRef.value.clearValidate()
  visible.value = true
}


// 设置数据
const setFormData = async (data) => {
  formData.room_id = data.room_id
  let res = await api.getPlayBack({room_id: data.room_id})
  if (res.code === 200) {
    for (const key in res.data.info) {
      if (res.data.info[key] != null && res.data.info[key] != undefined) {
        formData[key] = res.data.info[key]
      }
      if (key === 'play_back_days') {
        formData[key] = res.data.info[key]+''
      }
    }
  }
  
}

// 数据保存
const submit = async (done) => {
  const validate = await formRef.value?.validate()
  if (!validate) {
    loading.value = true
    let data = { ...formData }
    let result = {}
    if (mode.value === 'add') {
      // 添加数据
      data.id = undefined
      result = await api.setPlayBack(data)
    } else {
      // 修改数据
      result = await api.editBlockWord(data)
    }
    if (result.code === 200) {
      Message.success('操作成功')
      emit('success')
      done(true)
    }
    // 防止连续点击提交
    setTimeout(() => {
      loading.value = false
    }, 500)
  }
  done(false)
}

// 关闭弹窗
const close = () => (visible.value = false)

defineExpose({ open, setFormData })
</script>
